package com.carreirati.evaluation;

import java.util.List;

import com.carreirati.evaluation.domain.AppraisalRelationship;
import com.carreirati.evaluation.domain.Parameter;
import com.carreirati.evaluation.domain.Question;
import com.carreirati.evaluation.domain.QuestionParameter;
import com.carreirati.evaluation.domain.QuestionParameterPK;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/questionparameters")
@Controller
@RooWebScaffold(path = "questionparameters", formBackingObject = QuestionParameter.class)
@RooWebJson(jsonObject = QuestionParameter.class)
public class QuestionParameterController {
	  
		@RequestMapping(value = "/find{questionId}", headers = "Accept=application/json")
	    @ResponseBody
	    public ResponseEntity<java.lang.String> findJson(@PathVariable("questionId") long questionId) {
	        HttpHeaders headers = new HttpHeaders();
	        headers.add("Content-Type", "application/json; charset=utf-8");
	        List<QuestionParameter> result = QuestionParameter.findQuestionParametersByQuestionId(questionId);
	        return new ResponseEntity<String>(QuestionParameter.toJsonArray(result), headers, HttpStatus.OK);
	    }
		
		 @RequestMapping(value = "/update{parameterId}", headers = "Accept=application/json")
		 @ResponseBody
		    public ResponseEntity<String> updateFromJson(@PathVariable("parameterId") long parameterId,  @RequestParam("questionId") long questionId,@RequestBody String json) {
		        HttpHeaders headers = new HttpHeaders();
		        headers.add("Content-Type", "application/json");      
		        QuestionParameter qp = QuestionParameter.findQuestionParameters(questionId, parameterId);
		        QuestionParameter questionParameter = QuestionParameter.fromJsonToQuestionParameter(json);	
		        qp.setDescription(questionParameter.getDescription());
		        if (qp.merge() == null) {
		            return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
		        }
		        return new ResponseEntity<String>(headers, HttpStatus.OK);
		    }
		 
			 @RequestMapping(value = "/delete{parameterId}", headers = "Accept=application/json")
			 @ResponseBody
			    public ResponseEntity<String> deleteFromJson(@PathVariable("parameterId") long parameterId,  @RequestParam("questionId") long questionId,@RequestBody String json) {
				 QuestionParameter questionParameter = QuestionParameter.findQuestionParameters(questionId, parameterId);
		        HttpHeaders headers = new HttpHeaders();
		        headers.add("Content-Type", "application/json");
		        if (questionParameter == null) {
		            return new ResponseEntity<String>(headers, HttpStatus.NOT_FOUND);
		        }
		        questionParameter.remove();
		        return new ResponseEntity<String>(headers, HttpStatus.OK);
		    }
			 
			 @RequestMapping(value = "/create{questionId}", headers = "Accept=application/json")
			 @ResponseBody
			    public ResponseEntity<String> createFromJson(@PathVariable("questionId") long questionId,  @RequestParam("parameterId") long parameterId, @RequestBody String json) {
			        HttpHeaders headers = new HttpHeaders();
			        headers.add("Content-Type", "application/json");      
			        Parameter p = Parameter.findParameter(parameterId);
			        Question q = Question.findQuestion(questionId);
			        QuestionParameter questionParameter = QuestionParameter.fromJsonToQuestionParameter(json);	
			        questionParameter.setQuestionId(q);
			        questionParameter.setParameterId(p);
			        QuestionParameterPK id = new QuestionParameterPK(q.getId(),p.getId());
					questionParameter.setId(id );
			        questionParameter.persist();  
			       
			        return new ResponseEntity<String>(headers, HttpStatus.CREATED);
			    } 
}
